#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

void test0(){
    int * p = (int*)malloc(sizeof(int));
    *p = 100;
    free(p);

    int * p1 = new int(4);
    cout << *p1 << endl;
    delete p1;

    //new表达式不指定初始化的值
    //就会按照该类型的默认值进行初始化
    int * p2 = new int();
    cout << *p2 << endl;
    delete p2;

    //不加()无法确保进行了初始化
    /* int * p3 = new int; */
    /* cout << *p3 << endl; */

    char * p4 = new char();
    cout << *p4 << endl;
    delete p4;
}

void test1(){
    int * p = new int[10]();
    for(int i = 0; i < 10; ++i){
        p[i] = i + 1;
    }

    for(int i = 0; i < 10; ++i){
        cout << p[i] << " ";
    }
    cout << endl;
    delete [] p;
}

void test2(){
    int * p = new int[5]{1,2,3};
    for(int i = 0; i < 5; ++i){
        cout << p[i] << " ";
    }
    cout << endl;
    delete [] p;
}

void test3(){
    /* char * p = new char[10]{"hello"}; */
    char * p = new char[6]{'h','e','l','l','o'};
    cout << p << endl;
    printf("%p\n",p);
    delete [] p;
    p = nullptr;
    /* printf("%p\n",p); */

    /* char arr[] = "hello"; */

    const char * pstr = "hello";
    cout << sizeof(pstr) << endl;
    cout << strlen(pstr) << endl;

    char * p2 = new char[strlen(pstr) + 1]();
    strcpy(p2,pstr);
    cout << p2 << endl;
    delete [] p2;
    p2 = nullptr;
}

int main(void){
    test3();
    return 0;
}
